﻿(function ($) {
    if (!window.drGlobalExtend) { window.drGlobalExtend = {}; }
    window.drGlobalExtend.easyui = {
        datagrid: function (para, id) {
            var now = this;
            this.para = {
                iconCls: 'icon-site',
                width: 'auto',
                nowrap: false, //True 就会把数据显示在一行里。
                striped: true, //True 就把行条纹化。（即奇偶行使用不同背景色）
                collapsible: true, //可调节列宽
                pagination: true,
                rownumbers: true,
                autoSave: true,
            };
            //属性
            this.gridId = id;
            this.grid = undefined;
            this.currentElement = $("#" + this.gridId);
            this.currentEditIndex = undefined;
            //方法
            this.loadpost = function (para) {
                return this.currentElement.edatagrid('load', para);
            };
            this.loadData = function (data) {
                return this.currentElement.edatagrid('loadData', data);
            };
            this.getSelected = function () {
                return this.currentElement.edatagrid("getSelected");
            },
            this.getSelections = function () {
                return this.currentElement.edatagrid("getSelections");
            };
            this.validateRow = function (editIndex) {
                return this.currentElement.edatagrid("validateRow", editIndex);
            };
            this.selectRow = function (editIndex) {
                return this.currentElement.edatagrid('selectRow', editIndex);
            };
            this.getRows = function () {
                return this.currentElement.edatagrid("getRows");
            };
            this.deleteRow = function (index) {
                var now = this;
                ERP_confirm("确定要删除？", function (ret) {
                    if (ret) {
                        now.currentElement.edatagrid("deleteRow", index);
                        var datas = now.currentElement.edatagrid("getRows");
                        now.currentElement.edatagrid('loadData', datas);
                    }
                });
            };
            this.beginEdit = function (editIndex) {
                return this.currentElement.edatagrid('beginEdit', editIndex);
            };
            this.endEdit = function (editIndex) {
                return this.currentElement.edatagrid("endEdit", editIndex);
            };
            this.endEditing = function () {
                if (this.currentEditIndex == undefined) { return true; }
                if (this.validateRow(this.currentEditIndex)) {
                    this.endEdit(this.currentEditIndex);
                    this.currentEditIndex = undefined;
                    return true;
                } else {
                    return false;
                }
            };
            //初始化
            $.extend(this.para, para);
            this.grid = this.currentElement.edatagrid(this.para);
            return this;
        }
    };
})($);